package com.pwc.conversionTool.rest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.node.ObjectNode;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.IO.Reader;
import com.pwc.conversionTool.Manager.SoapUIManager;
import com.pwc.conversionTool.Manager.XMLConfigImpl;
import com.pwc.conversionTool.config.UploadFileView;
import com.pwc.conversionTool.csv.CsvHelper;
import com.pwc.conversionTool.relation.RelationManager;

import static com.pwc.conversionTool.IO.Writer.*;

@Controller
public class relationRest {

	private static final Logger logger = LoggerFactory.getLogger(relationRest.class);
	
	@Autowired
	public ServletContext context;
	
	@RequestMapping(value = "/rest/relation/createRelation", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody String setUserNamePassword(String filePath,String operationName,String relationName,String savePath) {
		
		try {
			new RelationManager(savePath).createRelation(filePath, operationName, relationName);
		} catch (Exception e) {
			//log4j
			logger.error("error in createRelation");
			logger.error(Arrays.toString(e.getStackTrace()));
			return "error";
		}
		return "success";
	} 
	
	@RequestMapping(value = "/rest/relation/getRelationContent", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE )
	public @ResponseBody String getRelationContent(String path) {
		String result="";
		try {
			 result=Reader.getStringFromFile(path);
		} catch (Exception e) {
			logger.error("error in getRelationContent");
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		return result;
	} 
	
	@RequestMapping(value = "/rest/relation/getFilesFromFolder", method = RequestMethod.POST,produces ="application/json")
	public @ResponseBody Map getFilesFromFolder(String path) throws DocumentException {
		Map<String, List<String>> hashMap = new HashMap<String, List<String>>();
		for(List<String> s:UploadFileView.initial(path).GetFiles()){
			for(String ss:s){
				String filePath=path+File.separator+ss;
				hashMap.put(filePath, s);
			}	
		}
		return hashMap;
	}
	
	@RequestMapping(value = "/rest/relation/editRelationFilter ", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE )
	public @ResponseBody void saveJson(String path,String saveData) {
		try{
			writeStringToFile(new File(path), saveData);
		     }catch(Exception e){
		    	 logger.error("error in editRelationFilter");
		    	 logger.error(Arrays.toString(e.getStackTrace()));
		     }
	} 

}
